package com.ruoyi.project.mgr.course.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mgr.course.domain.MgrCourse;
import com.ruoyi.project.mgr.course.service.IMgrCourseService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程Controller
 * 
 * @author .
 * @date 2020-02-11
 */
@Controller
@RequestMapping("/mgr/course")
public class MgrCourseController extends BaseController
{
    private String prefix = "mgr/course";

    @Autowired
    private IMgrCourseService mgrCourseService;
    @Autowired
    private IUserService userService;

    @GetMapping()
    public String course(ModelMap modelMap)
    {
        modelMap.put("users", userService.getTeacherUserList());
        return prefix + "/course";
    }

    /**
     * 查询课程列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MgrCourse mgrCourse)
    {
        startPage();
        List<MgrCourse> list = mgrCourseService.selectMgrCourseList(mgrCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MgrCourse mgrCourse)
    {
        List<MgrCourse> list = mgrCourseService.selectMgrCourseList(mgrCourse);
        ExcelUtil<MgrCourse> util = new ExcelUtil<MgrCourse>(MgrCourse.class);
        return util.exportExcel(list, "course");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        modelMap.put("users", userService.getTeacherUserList());
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MgrCourse mgrCourse)
    {
        MgrCourse course = new MgrCourse();
        course.setTeacherId(mgrCourse.getTeacherId());
        List<MgrCourse> courseList = mgrCourseService.selectMgrCourseList(course);
        if (courseList != null && courseList.size() > 0) {
            return error("一个教师只能教一门课程！");
        }
        course = mgrCourseService.selectByCourseName(mgrCourse.getCourseName());
        if (course != null) {
            return error("课程名不能重复！");
        }
        return toAjax(mgrCourseService.insertMgrCourse(mgrCourse));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap)
    {
        MgrCourse mgrCourse = mgrCourseService.selectMgrCourseById(courseId);
        List<User> userList = userService.getTeacherUserList();
        userList.stream().forEach(s -> {
            if (s.getUserId().equals(mgrCourse.getTeacherId())) {
                s.setFlag(true);
            }
        });
        mmap.put("users", userList);
        mmap.put("mgrCourse", mgrCourse);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MgrCourse mgrCourse)
    {
        MgrCourse course = new MgrCourse();
        course.setTeacherId(mgrCourse.getTeacherId());
        List<MgrCourse> courseList = mgrCourseService.selectMgrCourseList(course);
        if (courseList != null && courseList.size() > 0) {
            if (!courseList.stream().allMatch(s ->s.getCourseId().equals(mgrCourse.getCourseId()))) {
                return error("一个教师只能教一门课程！");
            }
        }
        course = mgrCourseService.selectByCourseName(mgrCourse.getCourseName());
        if (course != null && !course.getCourseId().equals(mgrCourse.getCourseId())) {
            return error("课程名不能重复！");

        }
        return toAjax(mgrCourseService.updateMgrCourse(mgrCourse));
    }

    /**
     * 删除课程
     */
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mgrCourseService.deleteMgrCourseByIds(ids));
    }


}
